package com.videoaggre.zcl.parse.impl

import android.net.Uri
import com.videoaggre.zcl.bean.EnterdeskClassify
import com.videoaggre.zcl.bean.EnterdeskImage
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.jsoup.Jsoup
import java.lang.Exception

object EnterdeskParse {
    @JvmStatic
    suspend fun parseImages(html: String?) = withContext(Dispatchers.IO) {
        try {
            val enterdeskImages = mutableListOf<EnterdeskImage>()
            Jsoup.parse(html).select(".mbig_pic_list_li img")?.forEach {
                enterdeskImages.add(
                    EnterdeskImage(
                        it.attr("src"),
                        it.attr("a_width").toInt(),
                        it.attr("a_height").toInt(),
                    )
                )
            }
            enterdeskImages
        } catch (e: Exception) {
            mutableListOf()
        }
    }

    @JvmStatic
    fun parseBrand(html: String?): MutableList<EnterdeskClassify> {
        val enterdeskImages = mutableListOf<EnterdeskClassify>()
        Jsoup.parse(html).select(".m_list_shouji_model_box1_ul2 a")?.forEach {
            kotlin.runCatching {
                val href = it.attr("href")
                val uri = Uri.parse(href.takeIf { it.startsWith("https:") } ?: "https:${href}")
                enterdeskImages.add(EnterdeskClassify(name = it.text(), path = uri.lastPathSegment ?: ""))
            }
        }
        return enterdeskImages
    }

}